import { getMenuApi, getRoleApi } from "../apis/homeApi"

export default {
    namespaced: true,
    state: {
        token: '',
        userInfo: {},
        menuList: [],
        roleList: []
    },
    getters: {
        token: state => state.token,
        userInfo: state => state.userInfo,
        menuList: state => state.menuList,
        roleList: state => state.roleList
    },
    
    mutations: {
        mutationToken(state, token) {
            state.token = token
        },
        mutationUserInfo(state, user) {
            state.userInfo = user
        },
        mutationMenuList(state, menu) {
            state.menuList = menu
        },
        mutationRoleList(state, role) {
            state.roleList = role
        }
    },

    actions:{
        // 1、框架搭建测试
          userInfoAct({commit},id){
            /*  Promise.resolve({
                id:'admin',
                name:'系统超级管理员',
             }).then((data)=>{
                commit('mutationUserInfo',data)
             }) */
             commit('mutationUserInfo',{id:'admin',name:'系统超级管理员'})
          },

          // 2、获取菜单及权限信息
          menuAndRoleAct({commit},ids){
                   Promise.all([
                    getRoleApi(ids),
                    getMenuApi()
                   ]).then((data)=>{

               
                       if(!!data[0]&& data[0].code === 200){
                             commit('mutationRoleList',data[0].data)
                       }
               
                       if(!!data[1] && data[1].code === 200){
                         commit('mutationMenuList',data[1].data)
                       }
                   })
          }

    }
}